package com.example.demo.controller;


import com.example.demo.identity.ChatMessages;
import com.example.demo.services.RedisService;
import com.example.demo.services.MongoService;
import com.example.demo.identity.GroupMsg;
import com.example.demo.services.StockInfoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class ChatController {

    private final RedisService chatService;
    @Autowired
    private StockInfoService stockInfoService;

    public ChatController(RedisService chatService) {
        this.chatService = chatService;
    }

    @Autowired
    private MongoService mongoService;

    //    获取聊天信息
    @GetMapping("/get-chat-message/{fromId}/{toId}")
    public HashMap<String, Object> getChatMessage(@PathVariable String fromId, @PathVariable String toId) {
        List<ChatMessages> messagesList = chatService.getAllMessages(fromId, toId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data", messagesList);
        return messagesList == null || messagesList.isEmpty() ? null : map;
    }
    //    获取群聊信息
    @GetMapping("/get-group-message/{groupId}")
    public ResponseEntity<?> getGroupMessage(@PathVariable("groupId") String groupId) {
        List<GroupMsg> groupMsgList = mongoService.getGroupMsg(groupId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data", groupMsgList);
        return groupMsgList != null && !groupMsgList.isEmpty() ? ResponseEntity.ok(map) : ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    }








    //    用户上线获取离线信息（私聊获取）
    @GetMapping("/get-off-online-friend-message/{receiverId}/{senderId}")
    public ResponseEntity<List<Map<String, Object>>> getOffOnLineFriendMessage(@PathVariable("receiverId") String receiverId, @PathVariable("senderId") String senderId) throws JsonProcessingException {
        String key = "stockInfo:" + receiverId + "-" + senderId;
        List<Map<String, Object>> onLineOffList = stockInfoService.getOffLineMessageInfo(key);
        return onLineOffList.isEmpty() ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(null) : ResponseEntity.ok(onLineOffList);
    }

    //    用户上线获取群离线信息（群聊）
    @GetMapping("/get-off-online-group-message/{groupId}/{userId}")
    public ResponseEntity<List<Map<String, Object>>> getOffOnlineGroupMessage(@PathVariable("groupId") String groupId, @PathVariable("userId") String userId) throws JsonProcessingException {
        String key = "offlineGroupMessages:" + groupId + "-:" + userId;
        List<Map<String, Object>> groupMeesages = stockInfoService.getOffLineGroupMessageInfo(key, groupId);
        return groupMeesages.isEmpty() ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(null) : ResponseEntity.ok(groupMeesages);
    }

}
